package com.automobile.dao;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import com.automobile.domain.User;

import java.util.List;

//@CacheNamespace(blocking = true)
@Repository
public interface UserDao {
	
	@Select("select * from user where id = #{userId}")
	@Results(id = "userAll",value = {
			@Result(property = "id",column = "id",id = true),
			@Result(property = "name",column = "name"),
			@Result(property = "phone",column = "phone"),
			@Result(property = "password",column = "password"),
			@Result(property = "birthday",column = "birthday"),
			@Result(property = "registrationTime",column = "registrationTime"),
			@Result(property = "date",column = "date"),
			@Result(property = "sex",column = "sex"),
			@Result(property = "img",column = "img"),
			@Result(property = "status",column = "status"),
			@Result(property = "code",column = "code"),
			//@Result(property = "sites", column = "id", many = @Many(select = "com.automobile.dao.SiteDao.findSiteByUserId",fetchType= FetchType.LAZY)),
			//@Result(property = "orderforms", column = "id", many = @Many(select = "com.automobile.dao.OrderformDao.findOrderformByUserId",fetchType= FetchType.LAZY)),
			@Result(property = "announces", column = "id", many = @Many(select = "com.automobile.dao.AnnounceDao.findAnnounceByUserId",fetchType= FetchType.LAZY)),
	})
	User findUserById(@Param("userId") int userId);//根据Id

    @Select("select * from user ")
    @ResultMap("userAll")
    List<User> findAll();

    @Select("select name from user where id = #{id}")
    String findUserNameById(int id);

    @Select("select * from user where id = #{id}")
    User findById(int id);
	
	@Select("select * from user where phone = #{phone}")
	@ResultMap("userAll")
	User findUserByPhoneAsPassword(String phone);//根据手机号和密码
	
	@Select("select * from user where email = #{email}")
	@ResultMap("userAll")
	User findUserByEmailAsPassword(String email);//根据邮箱和密码
	
	@Select("select * from user where code = #{code}")
	@ResultMap("userAll")
	User findByActivateUser(String activate);//通过激活码找用户，是否有这个激活存在
	
	@Select("select count(*) from user where phone = #{phone}")
	int findUserByPhone(String phone);//手机号作为用户名
	
	@Select("select count(*) from user where phone = #{phone}")
	int findUserByEmail(String email);//邮箱用于修改密码，所以邮箱必不可少
	
	@Insert("insert into user(name,phone,password,email,sex,birthday,status,code) "
			+ "values(#{name},#{phone},#{password},#{email},#{sex},#{birthday},#{status},#{code})")
	void save(User user);//保存
	
	@Update(" <script> update user set name = #{name}, email = #{email} ,sex = #{sex} <if test = 'img!=null'>,img = #{img}</if> where id = #{id}  </script>")
	void updateMessage(User user);//更新
	
	@Update("update user set status = #{status} ,code = #{code} where id = #{id}")
	void updateActivate(User user);
	
	@Update("update user set phone = #{phone} where id = #{id}")
	void updatePhone(@Param("id") int id, @Param("phone") String phone);
	
	@Update("update user set password = #{password} where phone = #{phone}")
	void updatePassword(@Param("phone") String phone, @Param("password") String password);
}
